﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomSecurity;
using GeneralLibs;
using BarCode;
using SOP;
using Lists.SOP;

namespace Portal
{
    public partial class UcWorkflow : UserControl
    {
        public delegate void OnRequestLogoutHandler();
        public event OnRequestLogoutHandler RequestLogout;

        public UcWorkflow()
        {
            InitializeComponent();
        }

        public delegate void CallFunctionHandler(object caller, string function);
        public event CallFunctionHandler CallFunction;

        private void UcWorkflow_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            foreach (Control control in this.Controls)
            {
                SetEnableStateRecuirement(control);
            }

            string tenDoanhNghiepVietHoa = Config.TenDoanhNghiep.ToUpper();

        }

        private void SetEnableStateRecuirement(Control control)
        {
            foreach (Control childControl in control.Controls)
            {
                SetEnableStateRecuirement(childControl);
            }
            if (control.ContextMenuStrip != null)
            {
                foreach (ToolStripItem item in control.ContextMenuStrip.Items)
                {
                    if (item.Tag is string)
                    {
                        item.Enabled = CustomSecurity.User.CurUser.CheckPrivilege(item.Tag as string, CustomSecurity.Right.Read);
                    }
                }
            }
            else
            if (control.Tag is string)
            {
                control.Enabled = CustomSecurity.User.CurUser.CheckPrivilege(control.Tag as string, CustomSecurity.Right.Read);
            }
        }

        protected void Call(string function)
        {
            if (CallFunction != null)
            {
                CallFunction(this, function);
            }
        }

        public virtual void ShowHideFunction()
        {
            if (User.CurUser == null)
            {
                this.Enabled = false;
                return;
            }
            else
            {
                this.Enabled = true;
                foreach (Control control in this.Controls)
                {
                    if (control is FlowLayoutPanel)
                    {
                        foreach (Control btnControl in (control as FlowLayoutPanel).Controls)
                        {
                            if (btnControl is Button && (btnControl.Tag is string))
                            {
                                Function function = FunctionList.GetByName(btnControl.Tag as String);
                                if (function == null)
                                    continue;
                                else
                                    btnControl.Visible = (function.PosibleRight & User.CurUser.GetRight(function.ID)) > 0;
                            }
                        }
                    }
                }
            }
        }

        private void customButton1_Click(object sender, EventArgs e)
        {
            //contextMenuBaoCaoBanHang.Show(btnShowContextBaoCaoBanHang, new Point(0, 0));
        }
        private void customButton2_Click(object sender, EventArgs e)
        {
            //contextMenuSoSOP.Show(btnShowContextBaoCaoSoCai, new Point(0, 0));
        }
        private void customButton3_Click(object sender, EventArgs e)
        {
            //contextMenuBaoCaoMuaHang.Show(btnShowContextBaoCaoMuHang, new Point(0, 0));
        }
        private void customButton4_Click(object sender, EventArgs e)
        {
            //contextMenuBaoCaoKho.Show(btnShowContextBaoCaoKho, new Point(0, 0));
        }
        private void customButton5_Click(object sender, EventArgs e)
        {
        }
        private void customButton6_Click(object sender, EventArgs e)
        {
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void tồnKhoTheoHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void tìnhHìnhTiêuThụMặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void tồnKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }


        private void muaHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void muaHàngTheoKháchHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void tồnMặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void bánHàngChiTiếtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void nhậtKýBánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêBánHàngLẻToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void tổngHợpHàngXuấtBánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void tờKhaiVATToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

     
        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổQuỹTiềnMặtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổCáiChứngTừGhiSổToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổCàiNKCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổChiTiếtCácTàiKhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổĐăngKýChứngTừGhiSổToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổNhậtKýChungToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổTổngHợpChữTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void côngNợBánVàMuaHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void bánHàngNhanhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêBánHàngTheoHóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem21_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem22_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngTồnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void báoCáoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void báoCáoTổngHợpHàngKýGửiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void báoCáoTổngHợpHàngTheoNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêTraCứuMuaHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêTraCứuBánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngTồnToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngMớiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêBánLẻToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổChiTiếtCôngNợKếtHợpPhảiThuPhảiTrảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêChiTiếtTheoTàiKhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngĐãKiểmKêToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêtraCứuNhậpXuấtTồnNhanhsốLiệuCóThểChưaChínhXácToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngChuyểnKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổTổngHợpCôngNợKếtHợpPhảiThuPhảiTrảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void kinhDoanhThôngMinhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêChiTiếtNhậpXuấtKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngMớiTheoKháchHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngBánKhuyếnMạiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêTồnTheoNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void báoCáoTổngHợpTiềnHàngTheoNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêtraCứuNhậpXuấtTồnTheoNhàCungCâpBưuCụcHNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //ReportGenerators.MuaHang.FrmThongKeNhapXuatTonTheoNCC_BuuCuc frm = new ReportGenerators.MuaHang.FrmThongKeNhapXuatTonTheoNCC_BuuCuc();
            //frm.Show();
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổChiTiềnDạngFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReportGenerators.QuanLyQuy.ChiTien.FrmNhatKyChiTien frm = new ReportGenerators.QuanLyQuy.ChiTien.FrmNhatKyChiTien();
            frm.Show();
        }

        private void thốngKêNgườiMuaHàngLẻToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void báoCáoToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêChiTiếtTheoTàiKhoảnToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêBánLẻTheoBácSỹKêĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }


        //For sổ SOP
        private void kêKhaiNhânSựLàmCôngTácChuyênMônToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổTheoDõiBệnhNhânToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổTheoDõiADRToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổMuaBánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void bảngThôngBáoThuốcBịThuHồiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổTheoDõiThuốcBịĐìnhChỉLưuHànhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổHàngThuHồiTạiNhàThuốcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổTheoDõiCácKhiếuNạiCủaBệnhNhânToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổTheoDõiCácNhàPhânPhốiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổNhậpThuốcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổKiểmSoátChấtLượngThuốcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void phiếuTheoDõiNhiệtĐộĐộẨmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void tileBCMuaThuoc_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            contextMenuBaoCaoMuaHang.Show(Cursor.Position);
        }

        private void tileBCBanThuoc_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            contextMenuBaoCaoBanHang.Show(Cursor.Position);
        }

        private void tileBCKho_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            contextMenuBaoCaoKho.Show(Cursor.Position);
        }

        private void tileSoSOP_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            contextMenuSoSOP.Show(Cursor.Position);
        }


        //Buttom Chính
        private void tileBanLe_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            Right right = User.CurUser.GetRight(tileBanLe.Tag.ToString());
            if (right == CustomSecurity.Right.None) return;

            BasicForms.FrmProgressBar.StartShowProgress();

            int desksHeights = Screen.PrimaryScreen.Bounds.Height;
            int desksWidths = Screen.PrimaryScreen.Bounds.Width;

            try
            {
                if (ModifierKeys == Keys.Control)
                {
                    BanLe.FrmDanhSachHoaDonBanLe frm = new BanLe.FrmDanhSachHoaDonBanLe();
                    frm.Show();
                }
                else
                {
                    if (desksHeights <= 768 && desksWidths <= 1025)
                    {
                        BanLe.FrmEditHoaDonBanLe frm = new BanLe.FrmEditHoaDonBanLe();
                        frm.Show();
                    }
                    else //if (760 <= desksHeights && 1024 <= desksWidths && desksWidths <= 1367)
                    {
                        BanLe.FrmEditHoaDonBanLe1366x768 frm = new BanLe.FrmEditHoaDonBanLe1366x768();
                        frm.Show();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            BasicForms.FrmProgressBar.FinishShowProgress();
        }

        private void tileMuaHang_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                tileMuaHang.Tag = "FrmDanhSachHoaDonMuaHang";
            }
            else
            {
                tileMuaHang.Tag = "FrmEditHoaDonMuaHang";
            }

            Call((sender as DevExpress.XtraEditors.TileItem).Tag as string);
        }

        private void tileNhapKho_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                tileNhapKho.Tag = "FrmDanhSachPhieuNhapKho";
            }
            else
            {
                tileNhapKho.Tag = "FrmEditPhieuNhapKho";
            }
            Call((sender as DevExpress.XtraEditors.TileItem).Tag as string);
        }

        private void tileHangMuaTraLai_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                tileHangMuaTraLai.Tag = "FrmDanhSachHangMuaTraLai";
            }
            else
            {
                tileHangMuaTraLai.Tag = "FrmEditHangMuaTraLai";
            }

            Call((sender as DevExpress.XtraEditors.TileItem).Tag as string);
        }

        private void tileBanThuoc_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                tileBanThuoc.Tag = "FrmDanhSachHoaDonBanHang";
            }
            else
            {
                tileBanThuoc.Tag = "FrmEditHoaDonBanHang";
            }

            Call((sender as DevExpress.XtraEditors.TileItem).Tag as string);
        }

        private void tilePhieuXuatKho_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                tilePhieuXuatKho.Tag = "FrmDanhSachPhieuXuatKho";
            }
            else
            {
                tilePhieuXuatKho.Tag = "FrmEditPhieuXuatKho";
            }

            Call((sender as DevExpress.XtraEditors.TileItem).Tag as string);
        }

        private void tileHangBanTraLai_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                tileHangBanTraLai.Tag = "FrmDanhSachHangBanTraLai";
            }
            else
            {
                tileHangBanTraLai.Tag = "FrmEditHangBanTraLai";
            }

            Call((sender as DevExpress.XtraEditors.TileItem).Tag as string);
        }

        private void tileItemDatThuoc_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            tileItemDatThuoc.Tag = "FrmDanhSachPhieuDatHang";
            Call((sender as DevExpress.XtraEditors.TileItem).Tag as string);
        }
    }
}
